/*
 * This is a  java script made by Speed from the RSBot forums
 * Made by Speed, please do not copy/distribute
 * Banking might have a little problem, if this occurs frequently, please contact me.
 * CHANGELOG FOR V1.25:
 * - You can withdraw from any where in the visible bank screen.
 * - Pineapple Rings or Chunks.
 */

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class SuperPizzas extends Script implements ServerMessageListener,
		PaintListener {

	public int[] bankBooth = new int[] { 11402, 35647, 36831 };

	public int startCook = 0;

	public int pizzacount = 0;

	public int startlvl = 0;

	public int pizzasmade = 0;

	public int fpizza = 0;
	public int plainpizza = 2289;
	public int ingredient = 0;
	public int fails = 0;
	public int number = 1;
	int pizzasleft;
	public String TOPPING;
	boolean making = false;
	int randomInt;
	int GambleInt;
	long lastCheck = 0;
	int checkTime;
	public long startTime = System.currentTimeMillis();

	public void antiban() {
		GambleInt = random(1, 20);
		if (GambleInt == 1) {
			turnCamera();
		}

		if (GambleInt == 2) {
			final int xA = random(0, 750);
			final int yA = random(0, 500);
			moveMouse(xA, yA);
			turnCamera();
		}

		if (GambleInt == 3) {
			if (getCurrentTab() != Constants.TAB_INVENTORY) {
				openTab(Constants.TAB_INVENTORY);
				turnCamera();
			}
		}

		if (GambleInt == 4) {
			clickCharacter(getNearestPlayerByLevel(1, 130), "Cancel");
			wait(random(500, 1750));
		}

		if (System.currentTimeMillis() - lastCheck >= checkTime) {
			lastCheck = System.currentTimeMillis();
			checkTime = random(300000, 600000);
			turnCamera();
			openTab(random(0, 13));
			turnCamera();
		}

		if (GambleInt == 5) {
			turnCamera();
			final int xA = random(0, 750);
			final int yA = random(0, 500);
			moveMouse(xA, yA);
		}

		if (GambleInt == 6) {
			turnCamera();
		}

		if (GambleInt == 7) {
			openTab(random(0, 13));
		}

		if (GambleInt == 8) {
			moveMouse(random(0, 450), random(0, 450));
		}
	}

	public void Bank() {

		final RSObject Bank = findObject(bankBooth);
		if (!RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()
				&& !isBusy() || Bank != null && !isBusy()) {
			atObject(Bank, "Use-Quickly");
			wait(random(625, 1545));
		}
		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()
				&& inventoryContains(fpizza)) {
			number = number - getInventoryCount(fpizza);
			wait(random(229, 360));
			making = false;
			bank.depositAll();
			wait(random(300, 500));
		}

		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()
				&& !inventoryContains(ingredient)) {
			log("Banking everything.");
			if (pizzasmade > 13) {
				wait(random(250, 500));
				bank.atItem(ingredient, "Withdraw-14");
				wait(random(500, 1000));
			} else {
				wait(random(250, 500));
				bank.atItem(ingredient, "Withdraw-X");
				wait(random(700, 1200));
				sendText("14", true);

				wait(random(1000, 1500));
			}

			if (!inventoryContains(plainpizza, ingredient)) {
				fails++;
				wait(random(400, 600));
				if (fails == 10) {
					log("Out of Plain Pizzas or " + TOPPING);
					log("Stopping script.");
					stopAllScripts();
				}
			}
		}
		if (inventoryContains(fpizza)) {
			fails = 0;
			wait(random(400, 600));
		}

		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()
				&& !inventoryContains(plainpizza)) {
			wait(random(250, 500));
			bank.atItem(plainpizza, "Withdraw-14");
			log("Withdrawing 14 Plain Pizzas");
			wait(random(500, 1000));
		}
	}

	@Override
	public String getAuthor() {
		return "Speed";
	}

	@Override
	public String getName() {
		return "Super Pizza Maker";
	}

	@Override
	public String getScriptCategory() {
		return "Cooking";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<body>";
		html += "<h2>" + getName() + " v" + getVersion() + "</h2><br>";
		html += "<b>Author:</b> " + getAuthor() + " <b><br/>\n";
		html += "</font>";
		html += "<b>Instructions:</b> Start in lumby, varrock west bank.<br>";
		html += "<b>PAINT IS HERE!<br></b>";
		html += "How many pizza's you wanna make?:<br> <input type=\"text\" name=\"number\"><br>";
		html += "<br><b>Anchovie pizza:<input type=\"radio\" name=\"pizza\" value=\"anchovies\" checked=\"checked\"></b>";
		html += "<b>Pinapple pizza (chunks):<input type=\"radio\" name=\"pizza\" value=\"pinapplechunks\"></b>";
		html += "<b>Pinapple pizza (rings):<input type=\"radio\" name=\"pizza\" value=\"pinapplerings\"></b>";
		html += "</body>\n";
		html += "</html\n";

		return html;
	}

	@Override
	public double getVersion() {
		return 1.25;
	}

	public boolean isBusy() {
		boolean flag = false;
		for (int i = 0; i < 4; i++) {
			if (getMyPlayer().getAnimation() != -1) {
				flag = true;
				break;
			}
			wait(random(200, 250));
		}
		return flag;
	}

	@Override
	public int loop() {
		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()
				&& inventoryContains(plainpizza)
				&& inventoryContains(ingredient)
				|| inventoryContains(plainpizza)
				&& inventoryContains(ingredient)) {
			MakePizza();
		}

		else {
			openTab(Constants.TAB_INVENTORY);
			Bank();
		}
		return random(500, 1000);

	}

	public void MakePizza() {

		if (RSInterface.getInterface(Constants.INTERFACE_BANK).isValid()) {
			bank.close();
		}

		if (!isBusy() && !RSInterface.getInterface(309).isValid()
				&& making == false) {
			wait(random(800, 1000));
			log("Making Pizzas.");
			usePizza();
		}
		if (inventoryContains(ingredient)) {
			making = false;
			wait(random(800, 1000));
		}

		if (RSInterface.getInterface(309).isValid()) {
			for (int i = 0; i < 4; i++) {
				// at problem with running again after random.
				atInterface(309, 6, "Make All");
				wait(random(1000, 1500));
				atMenu("Make All");
				wait(random(800, 1300));
				antiban();
			}
		}

		if (number <= 0) {
			log("Done what has been asked for.");
			stopAllScripts();
		}
		if (isBusy()) {

			wait(random(800, 1000));
		}

	}

	public void onRepaint(final Graphics g) {

		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			g.setColor(Color.RED);
			g.drawString("Super Pizza Maker by Speed", 9, 30);
			g.drawString("Pizza's made: " + pizzasmade, 9, 40);
			g.drawString("Time running: " + hours + ":" + minutes + ":"
					+ seconds + ".", 9, 60);

		}

	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		if (args.get("pizza").equals("anchovies")) {
			fpizza = 2297;
			ingredient = 319;
			TOPPING = "Anchovies";
		}
		if (args.get("pizza").equals("pinapplerings")) {
			fpizza = 2301;
			ingredient = 2118;
			TOPPING = "Pinapple Rings";
		}
		if (args.get("pizza").equals("pinapplechunks")) {
			fpizza = 2301;
			ingredient = 2116;
			TOPPING = "Pinapple Chunks";
		}

		number = Integer.parseInt(args.get("number"));
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		if (e.getMessage().contains("pizza")) {
			pizzasmade++;
			wait(random(500, 750));
			making = true;
		}
		if (e.getMessage().contains("space")) {
			bank.close();
			stopAllScripts();
			logout();
		}
	}

	// credits WarXperiment
	public void turnCamera() {
		final char[] LR = new char[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT };
		final char[] UD = new char[] { KeyEvent.VK_UP, KeyEvent.VK_DOWN };
		final char[] LRUD = new char[] { KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
				KeyEvent.VK_UP, KeyEvent.VK_DOWN };
		final int random2 = random(0, 2);
		final int random1 = random(0, 2);
		final int random4 = random(0, 4);

		if (random(0, 3) == 0) {
			Bot.getInputManager().pressKey(LR[random1]);
			try {
				wait(random(100, 400));
			} catch (final Exception e) {
			}
			Bot.getInputManager().pressKey(UD[random2]);
			try {
				wait(random(300, 600));
			} catch (final Exception e) {
			}
			Bot.getInputManager().releaseKey(UD[random2]);
			try {
				wait(random(100, 400));
			} catch (final Exception e) {
			}
			Bot.getInputManager().releaseKey(LR[random1]);
		} else {
			Bot.getInputManager().pressKey(LRUD[random4]);
			if (random4 > 1) {
				try {
					wait(random(300, 600));
				} catch (final Exception e) {
				}
			} else {
				try {
					wait(random(500, 900));
				} catch (final Exception e) {
				}
			}
			Bot.getInputManager().releaseKey(LRUD[random4]);
		}
	}

	public void usePizza() {
		atInventoryItem(ingredient, "Use");
		wait(random(129, 260));
		atInventoryItem(plainpizza, "Use");
		wait(random(129, 260));
		clickMouse(256, 407, 3, 3, false);
		wait(random(500, 600));
		atMenu("Make All");
	}

}